package com.atguigu.gmall.cart.service.impl;

import com.atguigu.gmall.cart.mapper.CartInfoMapper;
import com.atguigu.gmall.cart.service.CartInfoSyncService;
import com.atguigu.gmall.model.cart.CartInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class CartInfoSyncServiceImpl implements CartInfoSyncService {


    @Override
    @Async //异步注解
    public void updateSync(String userId, String userTempId, Long skuId) {
        System.out.println("updateSync 当前加入购物车的子线程："+Thread.currentThread().getName());
        //条件：用户ID +库存ID==唯一ID
        CartInfo cartInfo = new CartInfo();
        cartInfo.setUserId(userId);

        cartInfoMapper.update(cartInfo, new QueryWrapper<CartInfo>()
                .eq("user_id", userTempId)
                .eq("sku_id",skuId));
    }

    @Autowired
    private CartInfoMapper cartInfoMapper;
    //异步更新DB
    @Override
    @Async //异步注解
    public void updateSync(CartInfo cartInfo) {
        System.out.println("updateSync 当前加入购物车的子线程："+Thread.currentThread().getName());
        //条件：用户ID +库存ID==唯一ID
        cartInfoMapper.update(cartInfo,new QueryWrapper<CartInfo>()
                .eq("user_id",cartInfo.getUserId())
                .eq("sku_id",cartInfo.getSkuId()));
    }

    //异步添加DB
    @Override
    @Async //异步注解
    public void insertSync(CartInfo cartInfo) {
        System.out.println("insertSync:当前加入购物车的子线程："+Thread.currentThread().getName());
        cartInfoMapper.insert(cartInfo);
    }

    //更新DB
    @Override
    public void updateSyncBatch(String userId, String userTempId) {
        System.out.println("updateSyncBatch当前加入购物车的子线程：" + Thread.currentThread().getName());
        CartInfo cartInfo = new CartInfo();
        cartInfo.setUserId(userId);
        cartInfoMapper.update(cartInfo,new QueryWrapper<CartInfo>()
                .eq("user_id",userTempId));
    }

    /**
     * 删除
     * @param cartInfoByUserTempId
     */
    @Override
    @Async //异步注解
    public void deleteSync(CartInfo cartInfoByUserTempId) {
        System.out.println("deleteSync当前加入购物车的子线程：" + Thread.currentThread().getName());
        cartInfoMapper.delete(new QueryWrapper<CartInfo>()
                .eq("user_id",cartInfoByUserTempId.getUserId())
                .eq("sku_id",cartInfoByUserTempId.getSkuId()));
    }



}
